package cn.edu.zknu.mita.controller;

import cn.edu.zknu.mita.model.User;
import cn.edu.zknu.mita.service.IRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Timer;
import java.util.TimerTask;

@Controller
public class RegisterController {

    @Autowired
    private IRegisterService registerService;

    @RequestMapping("/register")
    public String register(){
        return "register";
    }

    @ResponseBody
    @RequestMapping("/sendSMS")
    public String getSMSCode(String tel, HttpSession session){
        return registerService.sendSMS(tel);
        /*session.setAttribute("CODE_IN_SESSION","1212121");
        return "5201314";*/
    }

    @ResponseBody
    @RequestMapping("/checkTel")
    public Boolean checkTel(String tel){
        return registerService.checkTel(tel);
    }

    @ResponseBody
    @RequestMapping("/checkUsername")
    public Boolean checkUsername(String username){
        return registerService.checkUsername(username);
    }

    //前端传来的application/json格式的数据，controller的参数必加RequestBody注解
    @ResponseBody
    @RequestMapping(value = "/submitReg",method = RequestMethod.POST)
    public String submitReg(@RequestBody User user){
        System.out.println(user);
        registerService.saveUser(user);
        return "注册成功";
    }
}
